import java.util.Vector;

/**
 * Class holding the structure of a North/South Road, which extends Road.
 * @author Jared Lindbl�m
 */
public class NorthSouthRoad extends Road {
	private Vector<Vehicle> northBound;
	private Vector<Vehicle> southBound;
	private enum Direction {North, South};

	/**
	 * Constructor.
	 * @param name			The name of the Road.
	 * @param weight		The weight of the road.
	 * @param maxVehicles 	The number of vehicles on the road.
	 */
	public NorthSouthRoad(String name, double distance, int weight, int maxVehicles) {
		super(name, distance, weight, maxVehicles, "North/South");
		northBound = new Vector<Vehicle>();
		southBound = new Vector<Vehicle>();
	}


	/**
	 * Returns the number of vehicles on the road.
	 * @param direction	The direction the vehicle is traveling.
	 * @return	The number of vehicles on the road.
	 */
	public int getNumVehicles (Direction direction) {
		int numVehicles = -1;

		switch (direction) {
		case North:
			numVehicles = northBound.size();
			break;
		case South:
			numVehicles = southBound.size();
			break;
		}

		return numVehicles;
	}

	/**
	 * Adds a vehicle in either the north or south bound direction.
	 * Checks to see if traffic has not exceeded maxVehicles.
	 * @param vehicle 		The vehicle to be added.
	 * @param direction 	The direction in which to add the vehicle.
	 * @return True/false if vehicle could be added.
	 */
	public boolean addVehicle(Vehicle vehicle, Direction direction) {
		boolean success = false;

		switch (direction) {
		case North:
			if (northBound.size() < maxVehicles) {
				northBound.addElement(vehicle);
				success = true;
			}
			break;
		case South:
			if (southBound.size() < maxVehicles) {
				southBound.addElement(vehicle);
				success = true;
			}
			break;
		}

		return success;
	}

	/**
	 * Removes a vehicle from either the north or south bound direction.
	 * @param vehicle 		The vehicle to be removed.
	 * @param direction 	The direction in which to remove the vehicle.
	 * @return True/false if vehicle could be removed.
	 */
	public boolean removeVehicle(Vehicle vehicle, Direction direction) {
		boolean success = false;

		switch (direction) {
		case North:
			success = northBound.removeElement(vehicle);
			break;
		case South:
			success = southBound.removeElement(vehicle);
			break;
		}

		return success;
	}

	/**
	 * Returns the next intersection for this road given the last intersection and current direction
	 * @param previousIntersection 	The intersection the vehicle last passed.
	 * @param direction 			The direction of the vehicle
	 * @return The next intersection the vehicle will hit.
	 */
	public Intersection nextIntersection(Intersection previousIntersection, Direction direction) {
		Intersection temp = null;
		int index = intersections.indexOf(previousIntersection);

		if (index > -1) {
			switch (direction) {
			case North:
				if ((index - 1) < intersections.size()) {
					temp = intersections.get(index - 1);
				}
				break;
			case South:
				if ((index + 1) >= 0) {
					temp = intersections.get(index + 1);
				}
				break;			
			}
		}

		return temp;
	}
}